<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="商户号" prop="merId" v-if="isAdmin">
        <el-input
          v-model="queryParams.merId"
          placeholder="请输入商户号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目编号" prop="projectId">
        <el-input
          v-model="queryParams.projectId"
          placeholder="请输入项目编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布日期" prop="releaseDate">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>



      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:project:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['project:project:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['project:project:export']"
        >导出
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="商户号" align="center" prop="merId" v-if="isAdmin"/>
      <el-table-column label="合同编码" align="center" prop="contractId"/>
      <el-table-column label="项目编号" align="center" prop="projectId"/>
      <el-table-column label="项目名称" align="center" prop="projectName"/>
      <el-table-column label="项目状态" align="center" prop="projectStatus"
                       :formatter="projectStatusFormat">
        <template slot-scope="scope">
          <span v-if="scope.row.projectStatus === '1'"><el-tag type="success" effect="dark"
                                                               size="mini">上架</el-tag></span>
          <span v-if="scope.row.projectStatus === '2'"><el-tag type="danger" effect="dark"
                                                               size="mini">下架</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column label="项目开始日期" align="center" prop="projectStartDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.projectStartDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目结束日期" align="center" prop="projectEndDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.projectEndDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布日期" align="center" prop="releaseDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.releaseDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleViewUpdate(scope.row,'view')"
          >查看
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleViewUpdate(scope.row,'edit')"
            v-hasPermi="['project:project:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:project:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="商户号" prop="merId" v-if="isAdmin">
          <el-input v-model="form.merId" placeholder="请输入商户号" :disabled="isEdit"/>
        </el-form-item>

        <el-form-item label="合同编码" prop="contractId">
          <el-select style="width:400px;" filterable v-model="form.contractId" placeholder="请选择合同编码" :disabled="isEdit">
            <el-option
              v-for="projectSign in projectSignOptions"
              :key="projectSign.contractId"
              :label="projectSign.contractId"
              :value="projectSign.contractId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName"  maxlength="10" placeholder="请输入项目名称"/>
        </el-form-item>
        <el-form-item label="项目状态" prop="projectStatus">
          <el-select v-model="form.projectStatus" placeholder="请选择项目状态">
            <el-option
              v-for="dict in projectStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职位薪资" prop="positionSalary">
          <el-input v-model="form.positionSalary"  maxlength="11" placeholder="格式为: 10-20K"/>
        </el-form-item>
        <el-form-item label="薪资类型" prop="salaryType">
          <el-select v-model="form.salaryType" placeholder="请选择薪资类型">
            <el-option
              v-for="dict in salaryTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="薪资单位" prop="salaryUnit">
          <el-select v-model="form.salaryUnit" placeholder="请选择薪资单位">
            <el-option
              v-for="dict in salaryUnitOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="性别要求" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别要求">
            <el-option
              v-for="dict in sexOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄要求" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄要求"/>
        </el-form-item>
        <el-form-item label="学历要求" prop="education">
          <el-select v-model="form.education" placeholder="请选择学历要求">
            <el-option
              v-for="dict in educationOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作内容" prop="workDesc">
          <el-input v-model="form.workDesc" type="textarea" placeholder="请输入工作内容"/>
        </el-form-item>
        <el-form-item label="薪资说明" prop="salaryDesc">
          <el-input v-model="form.salaryDesc" type="textarea" placeholder="请输入薪资说明"/>
        </el-form-item>
        <el-form-item label="工作时间" prop="workDate">
<!--          <el-input v-model="form.workDate" placeholder="请输入工作时间"/>-->
          <el-date-picker
            v-model="form.workDate"
            type="datetimerange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-mm-dd hh:mm:ss"
            :time-arrow-control="true"
            :picker-options="pickerOptions"
            @change="getStime">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="工作地点" prop="workAddr">
          <el-input v-model="form.workAddr" placeholder="请输入工作地点"/>
        </el-form-item>
        <el-form-item label="项目开始日期" prop="projectStartDate">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.projectStartDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择项目开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="项目结束日期" prop="projectEndDate">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.projectEndDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择项目结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发布日期" prop="releaseDate">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.releaseDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择发布日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="title !== '查看项目'">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {listProject, getProject, delProject, addProject, updateProject, exportProject} from "@/api/project/project";
  import {getInfo} from "@/api/login";
  import {listProjectSign} from "@/api/sign/projectSign";
  export default {
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        merIds: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        //是否是编辑状态
        isEdit: true,
        loginMerId:"",
        isAdmin: true,
        // 日期范围
        dateRange: [],
        // 项目信息表格数据
        projectList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 项目状态 1：上架 2：下架字典
        projectStatusOptions: [],
        // 薪资类型 日结、月结、项目结字典
        salaryTypeOptions: [],
        // 薪资单位 月 日 次 单字典
        salaryUnitOptions: [],
        // 性别要求 男、女、男女不限字典
        sexOptions: [],
        // 学历要求字典
        educationOptions: [],
        //项目签约列表
        projectSignOptions:[],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          merId: undefined,
          projectId: undefined,
          projectName: undefined,
          releaseDate: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          merId: [
            {required: true, message: "商户号不能为空", trigger: "blur"}
          ], contractId: [
            {required: true, message: "合同编码不能为空", trigger: "blur"}
          ], projectName: [
            {required: true, message: "项目名称不能为空", trigger: "blur"}
          ], projectStatus: [
            {required: true, message: "项目状态不能为空", trigger: "blur"}
          ], positionSalary: [
            {required: true, message: "职位薪资不能为空", trigger: "blur"}
          ], salaryType: [
            {required: true, message: "薪资类型不能为空", trigger: "change"}
          ], salaryUnit: [
            {required: true, message: "薪资单位不能为空", trigger: "change"}
          ], sex: [
            {required: true, message: "性别要求不能为空", trigger: "change"}
          ], age: [
            {required: true, message: "年龄要求不能为空", trigger: "blur"}
          ], education: [
            {required: true, message: "学历要求不能为空", trigger: "blur"}
          ], workDesc: [
            {required: true, message: "工作内容不能为空", trigger: "blur"}
          ], salaryDesc: [
            {required: true, message: "薪资说明不能为空", trigger: "blur"}
          ], workDate: [
            {required: true, message: "工作时间不能为空", trigger: "blur"},
          ], workAddr: [
            {required: true, message: "工作地点不能为空", trigger: "blur"}
          ], projectStartDate: [
            {required: true, message: "项目开始日期不能为空", trigger: "blur"}
          ], projectEndDate: [
            {required: true, message: "项目结束日期不能为空", trigger: "blur"}
          ], releaseDate: [
            {required: true, message: "发布日期不能为空", trigger: "blur"}
          ],
        }
      };
    },
    created() {
      getInfo().then(response => {
        this.loginMerId = response.user.merId;
        this.isAdmin = response.user.admin;
      });
      this.getList();
      this.getContractList();
      this.getDicts("project_status").then(response => {
        this.projectStatusOptions = response.data;
      });
      this.getDicts("salary_type").then(response => {
        this.salaryTypeOptions = response.data;
      });
      this.getDicts("salary_unit").then(response => {
        this.salaryUnitOptions = response.data;
      });
      this.getDicts("sex").then(response => {
        this.sexOptions = response.data;
      });
      this.getDicts("education").then(response => {
        this.educationOptions = response.data;
      });
    },
    methods: {
      /** 查询项目信息列表 */
      getList() {
        this.loading = true;
        listProject(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.projectList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 查询列表 */
      getContractList(){
        listProjectSign({"serverMerId":this.loginMerId}).then(response => {
          this.projectSignOptions = response.rows;
        });
      },
      // 项目状态 1：上架 2：下架字典翻译
      projectStatusFormat(row, column) {
        return this.selectDictLabel(this.projectStatusOptions, row.projectStatus);
      },
      // 薪资类型 日结、月结、项目结字典翻译
      salaryTypeFormat(row, column) {
        return this.selectDictLabel(this.salaryTypeOptions, row.salaryType);
      },
      // 薪资单位 月 日 次 单字典翻译
      salaryUnitFormat(row, column) {
        return this.selectDictLabel(this.salaryUnitOptions, row.salaryUnit);
      },
      // 性别要求 男、女、男女不限字典翻译
      sexFormat(row, column) {
        return this.selectDictLabel(this.sexOptions, row.sex);
      },
      // 学历要求字典翻译
      educationFormat(row, column) {
        return this.selectDictLabel(this.educationOptions, row.education);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          merId: undefined,
          contractId: undefined,
          projectId: undefined,
          projectName: undefined,
          projectStatus: undefined,
          positionSalary: undefined,
          salaryType: undefined,
          salaryUnit: undefined,
          sex: undefined,
          age: undefined,
          education: undefined,
          workDesc: undefined,
          salaryDesc: undefined,
          workDate: undefined,
          workAddr: undefined,
          projectStartDate: undefined,
          projectEndDate: undefined,
          releaseDate: undefined,
          corpCode: undefined,
          corpName: undefined,
          createBy: undefined,
          createDate: undefined,
          reqIp: undefined,
          status: "0",
          updateBy: undefined,
          updateDate: undefined,
          remarks: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.merIds = selection.map(item => item.merId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.isEdit = false;
        this.open = true;
        this.title = "添加项目信息";
      },
      /** 修改按钮操作 */
      handleViewUpdate(row, type) {
        this.reset();
        const id = row.id || this.ids
        getProject(id).then(response => {
          this.form = response.data;
          this.open = true;
          if ('edit' === type) {
            this.isEdit = true;
            this.title = "修改项目";
          }
          if ('view' === type) {
            this.isEdit = false;
            this.title = "查看项目";
          }
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateProject(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              if(this.form.projectStartDate >this.form.projectEndDate){
                this.msgError("项目的开始时间不能大于项目的结束时间");
              }else{
                addProject(this.form).then(response => {
                  if (response.code === 200) {
                    this.msgSuccess("新增成功");
                    this.open = false;
                    this.getList();
                  } else {
                    this.msgError(response.msg);
                  }
                });
              }

            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        const merIds = row.merId || this.merIds;
        this.$confirm('是否确认删除数据?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delProject(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function () {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出项目信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return exportProject(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function () {
        });
      }
    }
  };
</script>
